//
//  Status.swift
//  开心微博
//
//  Created by 郑开心 on 15/12/10.
//  Copyright © 2015年 郑开心. All rights reserved.
//

import UIKit
import SwiftyJSON

class Status: NSObject {
    
    var retweeted_status : Status?
    
    //
    var user : User!
    //创建时间
    var created_at : String!
    
    //微博名
    var idstr : String!
    
    //微博信息内容
    var text : String!
    
    //微博来源
    var source : String!
    
    //转发数
    var reposts_count : Int!
    
    //评论数
    var comments_count : Int!
    
    //赞
    var attitudes_count : Int!
    
    //缩略图
    var pic_urls : [Photo]?
    

    
    init(json: JSON) {
        super.init()
        self.created_at = json["created_at"].stringValue
        self.attitudes_count = json["attitudes_count"].intValue
        self.comments_count = json["comments_count"].intValue
        self.reposts_count = json["reposts_count"].intValue
        self.source = json["source"].stringValue
        self.idstr = json["idstr"].stringValue
        self.text = json["text"].stringValue
        let re_status = json["retweeted_status"]
        if re_status != nil {
            self.retweeted_status = Status(json: re_status)
        }
        let user = json["user"]
        self.user = User(json: user)
        let photos = json["pic_urls"].arrayValue
        if photos.count > 0 {
            var pic_urls = [Photo]()
            for photo in photos {
                pic_urls.append(Photo(json: photo))
            }
            self.pic_urls = pic_urls
        }
        self.setSource()
        self.setRetName()
    }

    func getcreated_at() -> String {
        let fmt = NSDateFormatter()
        fmt.dateFormat = "EEE MMM d HH:mm:ss Z yyyy"
        fmt.locale = NSLocale(localeIdentifier: "en_US")
        let created_date = fmt.dateFromString(created_at)!
        if created_date.isThisYear() {
            if created_date.isToday() {
                let cmp = created_date.deltaWithNow()
                if cmp.hour >= 1 {
                    return "\(cmp.hour)小时之前"
                }else if cmp.minute > 1 {
                    return "\(cmp.minute)分钟之前"
                }else{
                    return "刚刚"
                }
            }else if created_date.isYesterday(){
                fmt.dateFormat = "昨天 HH:mm"
                return fmt.stringFromDate(created_date)
            }else{
                fmt.dateFormat = "yyyy-MM-dd HH:mm"
                return fmt.stringFromDate(created_date)
            }
        }else{
            fmt.dateFormat = "yyyy-MM-dd HH:mm"
            return fmt.stringFromDate(created_date)
        }
    }
    
    func setSource(){
        if let range = source.rangeOfString(">") {
            source = source.substringFromIndex(range.endIndex)
            if let range = source.rangeOfString("<") {
                source = source.substringToIndex(range.startIndex)
                source = "来自" + source
            }
        }
    }
    
    func setRetName(){
        if let name = retweeted_status?.user.name {
            retweeted_status?.user.name = "@" + name
        }
        
    }
        
}


